package api

import (
	"github.com/gin-gonic/gin"
	"server/global"
	"server/internal/middleware"
	"server/internal/model"
	"server/internal/pkg/response"
	"server/internal/services"
	"server/utils/tools"
)

// UserApi 用户管理
var UserApi = new(userApi)

type userApi struct {
	model.User
}

// Router 路由
func (a userApi) Router(r *gin.RouterGroup) {
	api := r.Group("/user", middleware.LoginVerification, middleware.PermissionVerification)
	//验证权限
	api.GET("/list", a.list)
	api.GET("/edit", a.detail)
	api.POST("/add", a.add)
	api.POST("/edit", a.edit)
	api.POST("/del", a.del)
}

// List 列表
func (userApi) list(c *gin.Context) {
	Db := global.GL_DB          //初始化Db
	Db = services.Query(Db, c)  //查询构造
	Db = services.Paging(Db, c) //分页

	//快速搜索
	quickSearch := c.Query("quickSearch")
	if quickSearch != "" {
		Db = Db.Where("username like ? or name like ?", "%"+quickSearch+"%", "%"+quickSearch+"%")
	}

	//Db.Where("user.is_admin=?", "1")
	list, total := services.User.GetList(Db)

	response.SuccessList(c, list, total)
}

// Detail 单条
func (userApi) detail(c *gin.Context) {
	id := c.Query("id")

	detail := services.User.GetById(id)

	response.SuccessData(c, detail, "")
}

// Add 添加
func (a userApi) add(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		response.Error(c, "参数错误", err.Error())
		return
	}
	a.IsAdmin = "1"
	a.Gender = "0"

	userInfo, _ := c.Get("userInfo")
	if userInfo.(model.User).TissueId == a.TissueId && a.GroupId == 2 {
		response.Error(c, "不能设置同级组织的帐号为管理员")
		return
	}

	if _, err := services.User.Create(a.User); err == nil {
		response.Success(c, "", "添加成功")
	} else {
		response.Error(c, "添加失败")
	}
}

// Edit 编辑
func (a userApi) edit(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		response.Error(c, "参数错误", err.Error())
		return
	}

	userInfo, _ := c.Get("userInfo")
	if userInfo.(model.User).TissueId == a.TissueId && a.GroupId == 2 {
		response.Error(c, "不能设置同级组织的帐号为管理员")
		return
	}

	if a.Password != "" {
		a.Password = tools.GetMd5String(a.Password)
	}
	a.Username = ""
	if _, err := services.User.Update(a.User); err == nil {
		response.Success(c, "", "编辑成功")
	} else {
		response.Error(c, "编辑失败", err.Error())
	}
}

// Del 删除
func (a userApi) del(c *gin.Context) {
	type Param struct {
		Ids []int `json:"ids"`
	}
	var param Param
	if err := c.ShouldBindJSON(&param); err != nil {
		response.Error(c, "参数错误", err.Error())
		return
	}

	if services.User.Delete(param.Ids) == nil {
		response.Success(c, "", "删除成功")
	} else {
		response.Error(c, "删除失败")
	}
}

// GetUserInfo 当前账号信息
func (a userApi) getUserInfo(c *gin.Context) {
	userInfo, _ := c.Get("userInfo")

	detail := services.User.GetById(userInfo.(model.User).ID)

	response.Success(c, detail, "")
}

// UserEdit 修改个人信息
func (a userApi) userEdit(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		response.Error(c, "参数错误", err.Error())
		return
	}
	userInfo, _ := c.Get("userInfo")
	a.ID = userInfo.(model.User).ID
	if a.Password != "" {
		a.Password = tools.GetMd5String(a.Password)
	}
	if _, err := services.User.Update(a.User); err == nil {
		response.Success(c, "", "编辑成功")
	} else {
		response.Error(c, "编辑失败")
	}
}
